package com.qdsgjsfk.vision.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.databinding.ActivityDeviceListBinding;

import com.qdsgjsfk.vision.adapter.DeviceTypeAdapter;
import com.rest.business.RestProxy;
import com.rest.response.DeviceResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.NetUtil;
import com.qdsgjsfk.vision.viewmodel.DeviceViewModel;

/**
 * 视力表或者验光仪型号页面
 * Created by HP on 2019/10/30.
 */

public class DeviceTypeActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private DeviceTypeAdapter deviceTypeAdapter;
    private List<DeviceResponse.Device> list = new ArrayList<>();


    @Override
    protected void initViews() {
        LinearLayoutManager managerOffline = new LinearLayoutManager(this);
        managerOffline.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(managerOffline);
        deviceTypeAdapter = new DeviceTypeAdapter(this, list);
        recyclerView.setAdapter(deviceTypeAdapter);
        deviceTypeAdapter.setOnItemClickLitener((type, name) -> {
            Intent bundle = new Intent(DeviceTypeActivity.this, DeviceListActivity.class);
            bundle.putExtra("type", type);
            bundle.putExtra("name", name);
            startActivity(bundle);
            finish();
        });
    }

    @Override
    protected void initData() {
        RestProxy.getInstance().getDevicesType(new Observer<DeviceResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DeviceResponse deviceResponse) {
                list.addAll(deviceResponse.data);
                deviceTypeAdapter.notifyDataSetChanged();
//                mItems.setValue( deviceResponse.data );
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                NetUtil.onError(e, DeviceTypeActivity.this);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_device_type;
    }

}
